﻿using System.Web.Mvc;
using Kratos.Framework.ContentManagement;
using Kratos.Framework.ContentManagement.Models.SystemRights;
using Kratos.Framework.ContentManagement.ViewModels;
using Kratos.Framework.ExtJsFramework;
using Kratos.Framework.Filters;
using Kratos.Framework.Infrastructures;
using Kratos.Framework.Modules.Core.Controllers;
using Kratos.Framework.Modules.Core.Services;

namespace Kratos.Modules.Admin.Controllers
{
    public class GroupController : KratosController
    {
        private readonly IGroupService _groupSrv;

        public GroupController(IGroupService groupSrv)
        {
            _groupSrv = groupSrv;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetList(int? pageIndex, int? pageSize)
        {
            int totalCount;
            var pageList = _groupSrv.GetListByPage(pageIndex, pageSize, out totalCount);
            return KratosJson(new PageData<Group> { TotalCount = totalCount, Data = pageList });
        }

        public ActionResult GetGroup(int id)
        {
            var group = _groupSrv.Get(id);
            return KratosJson(group);
        }

        [UnitOfWork]
        public ActionResult EditGroup(Group group)
        {
            ExtResult extResult;
            if (!group.Id.HasValue)
            {
                extResult = new ExtResult { Success = true, Msg = "传入的数据有误" };
            }
            else
            {
                _groupSrv.Update(group);
                extResult = new ExtResult { Success = true, Msg = "修改成功" };
            }
            return KratosJson(extResult);
        }

        [UnitOfWork]
        public ActionResult DeleteGroup(int id)
        {
            ExtResult extResult;
            var model = _groupSrv.Get(id);
            if (model == null)
                extResult = new ExtResult { Success = false, Msg = "记录不存在！" };
            else
            {
                _groupSrv.Delete(model);
                extResult = new ExtResult { Success = true, Msg = "删除成功！" };
            }

            return KratosJson(extResult);
        }

        public ActionResult GroupState()
        {
            var result = EnumDescription.ToList<GroupState>();
            return KratosJson(result, JsonRequestBehavior.AllowGet);
        }

        [UnitOfWork]
        public ActionResult AddGroup(Group group)
        {
            ExtResult extResult;
            if (!ModelState.IsValid)
            {
                extResult = new ExtResult
                                {
                                    Success = false,
                                    Msg = "添加失败!"
                                };
            }
            else
            {
                _groupSrv.Save(group);
                extResult = new ExtResult
                                    {
                                        Success = true,
                                        Msg = "添加成功"
                                    };
            }
            return KratosJson(extResult);
        }
    }
}
